﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Mammoth.Entities;
using Mammoth.Events;
using Mammoth.Services;

namespace Mammoth.ViewModels
{
    public class EventListViewModel : WPFViewModelBase
    {
        public static readonly RoutedCommand RemoveCommand = new RoutedCommand();
        public static readonly RoutedCommand ApproveCommand = new RoutedCommand();

        const int MaxStack = 50;

        public IList<EventItemEntity> EventList { get; set; }

        public EventListViewModel(IInputElement dispatcher)
            : base(dispatcher)
        {
            AddHandler(RoutedEvents.EventItemEvent,
                new RoutedEventHandler<EventItemEventArgs>(OnEventItemHandler));
            EventList = new ObservableCollection<EventItemEntity>();
        }

        public void RemoveHandler(object sender, ExecutedRoutedEventArgs e)
        {
            var evnt = (EventItemEntity)e.Parameter;
            EventList.Remove(evnt);
        }

        public void ApproveHandler(object sender, ExecutedRoutedEventArgs e)
        {
            var evnt = (SubscriptionEventItemEntity)e.Parameter;
            RemoveHandler(sender, e);
        }

        private void OnEventItemHandler(object sender, EventItemEventArgs e)
        {
            if (EventList.Count == MaxStack)
                EventList.RemoveAt(MaxStack - 1);
            EventList.Insert(0, e.EventItem);
        }
    }
}
